package uk.ac.glasgow.demosocs.tests;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.ElectionResult;
import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.VotingSystemStub;

public class PublishResults {
	
	private VotingSystem testVS;

	@Before
	public void setUp() throws Exception {
		testVS = new VotingSystemStub();

		testVS.setCurrentUser("admin", "p");
		testVS.createElection("e1", "Simple_Plurality");
		testVS.editElection(new Date(), new Date(), new Date(), "Level_3");
	}

	@After
	public void tearDown() throws Exception {
		testVS = null;
	}

	@Test
	/**
	 * tests publishResults with only one candidate
	 */
	public void oneCandidatePublish() {
		
		String c1 = "1";
		String eid = testVS.getCurrentElection().getEID();
				
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "Level_3");
		
		testVS.setCurrentElection(eid);
		testVS.nominateCandidate(c1);
		
		testVS.setCurrentUser("1", "p");		
		testVS.acceptNomination();
			
		testVS.setCurrentUser("admin", "p");
		testVS.editElection(new Date(), new Date(), new Date(), "Level_3");
		testVS.setCurrentElection(eid);
		
		testVS.publishResults();
		ElectionResult r = testVS.getCurrentElection().getElectionResult();
		
		String cwin;
		if (r != null) {
			cwin = r.getWinningOptions().get(0).getDisplayableDescription();
		}
		else {
			cwin = null;
		}
		
		assertEquals(c1, cwin);
		
	}
	
	@Test
	/**
	 * tests publishResults with only one candidate
	 */
	public void getWinnerBeforePublish() {
		
		String c1 = "1";
						
		String eid = testVS.getCurrentElection().getEID();
				
		Date startD = new Date();
		startD.setTime(startD.getTime() + 1000000000000000000L);
		testVS.editElection(new Date(), startD, startD, "Level_3");
		
		testVS.setCurrentElection(eid);
		testVS.nominateCandidate(c1);
		
		testVS.setCurrentUser("1", "p");		
		testVS.acceptNomination();
				
		testVS.editElection(new Date(), new Date(), new Date(), eid);
		testVS.setCurrentElection(eid);
		
		
		ElectionResult r = testVS.getCurrentElection().getElectionResult();
		
		
		assertEquals(null, r);
		
	}

}
